package com.ruoyi.patrol.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.patrol.domain.PtSite;
import com.ruoyi.patrol.service.IPtSiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 巡逻点位Controller
 * 
 * @author lkm
 * @date 2022-11-04
 */
@RestController
@RequestMapping("/patrol/site")
public class PtSiteController extends BaseController
{
    @Autowired
    private IPtSiteService ptSiteService;

    /**
     * 查询巡逻点位列表
     */
    @PreAuthorize("@ss.hasPermi('patrol:site:list')")
    @GetMapping("/list")
    public TableDataInfo list(PtSite ptSite)
    {
        startPage();
        List<PtSite> list = ptSiteService.selectPtSiteList(ptSite);
        return getDataTable(list);
    }

    /**
     * 导出巡逻点位列表
     */
    @PreAuthorize("@ss.hasPermi('patrol:site:export')")
    @Log(title = "巡逻点位", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PtSite ptSite)
    {
        List<PtSite> list = ptSiteService.selectPtSiteList(ptSite);
        ExcelUtil<PtSite> util = new ExcelUtil<PtSite>(PtSite.class);
        return util.exportExcel(list, "site");
    }

    /**
     * 获取巡逻点位详细信息
     */
    @PreAuthorize("@ss.hasPermi('patrol:site:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(ptSiteService.selectPtSiteById(id));
    }

    /**
     * 新增巡逻点位
     */
    @PreAuthorize("@ss.hasPermi('patrol:site:add')")
    @Log(title = "巡逻点位", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PtSite ptSite)
    {
        // 获取当前的用户
        Long schoolId = SecurityUtils.getSchoolId();
        if (StringUtils.isNull(schoolId)) {
            return AjaxResult.error("非学校管理员不可添加学校的巡逻点位");

        }

        ptSite.setSchoolId(schoolId);
        if (ptSiteService.siteNameExist(ptSite)) {
            return AjaxResult.error("本校已有重名的巡逻点位！");
        }

        return toAjax(ptSiteService.insertPtSite(ptSite));
    }

    /**
     * 修改巡逻点位
     */
    @PreAuthorize("@ss.hasPermi('patrol:site:edit')")
    @Log(title = "巡逻点位", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PtSite ptSite)
    {
        PtSite query = new PtSite();
        query.setSchoolId(ptSite.getSchoolId());
        query.setSiteName(ptSite.getSiteName());
        List<PtSite> list = ptSiteService.selectPtSiteListOther(query);
        long count = list.stream().filter(e -> !e.getId().equals(ptSite.getId())).count();
        if (count > 0) {
            return AjaxResult.error("本校已有重名的巡逻点位！");
        }

        return toAjax(ptSiteService.updatePtSite(ptSite));
    }

    /**
     * 删除巡逻点位
     */
    @PreAuthorize("@ss.hasPermi('patrol:site:remove')")
    @Log(title = "巡逻点位", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        if (ptSiteService.isSiteInUse(ids[0])) {
            return AjaxResult.error("有巡查线路使用本巡逻点位，暂不能删除！");
        }
        return toAjax(ptSiteService.deletePtSiteByIds(ids));
    }

    @GetMapping("/optionselect")
    public AjaxResult optionselect(PtSite ptSite)
    {
        List<PtSite> list = ptSiteService.selectPtSiteList(ptSite);
        return AjaxResult.success(list);
    }
}
